<template>
	<el-dialog title="提交实验结果" :visible.sync="visible" width="560px">
		<el-form size="medium" ref="formData" :model="formData" :rules="formRules" label-width="90px" class="summary_container">
			<el-row :gutter="16">
				<el-col :span="12">
					<el-form-item label="完成时间" prop="endTime">
						<el-date-picker v-model="formData.endTime" v-bind="datePickConfig"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="实际机时" prop="machineHour">
						<el-input-number v-model="formData.machineHour" controls-position="right" placeholder="输入实际机时" style="width: 100%"></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="完成情况" prop="content">
				<el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" v-model="formData.content"></el-input>
			</el-form-item>
			<el-form-item label="小结模板">
				<el-button class="temp" type="text" @click="handleDownloadTemp(3)">实验小结模板</el-button>
			</el-form-item>
			<el-form-item label="实验结果" prop="filed5">
				<gx-file-uploader type="6" v-model="formData.filed5"></gx-file-uploader>
			</el-form-item>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" @click="handleSubmitForm()" :loading="btnLoading">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import moment from 'moment';
import { piStatReality } from '@/api/business/tbPiStat';
export default {
	props: {
		value: { type: Boolean, default: false },
		data: { type: Object, default: () => {} }
	},
	data() {
		return {
			visible: this.value,
			loading: false,
			formData: {},
			formRules: {
				endTime: { required: true, message: '请选择完成时间', trigger: 'change' },
				machineHour: { required: true, message: '请输入实际机时', trigger: ['blur', 'change'] },
				content: { required: true, message: '请输入完成情况', trigger: 'blur' },
				filed5: { required: true, message: '请上传实验结果', trigger: 'change' }
			},
			btnLoading: false,
			datePickConfig: {
				type: 'date',
				clearable: false,
				style: { width: '100%' },
				valueFormat: 'yyyy-MM-dd',
				placeholder: '选择完成日期',
				pickerOptions: {
					disabledDate: this.formatDisabledDate
				}
			}
		};
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		async visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			this.$nextTick(() => {
				this.$refs['formData'].resetFields();
				this.formData = {};
			});
		}
	},
	methods: {
		//
		formatDisabledDate(time) {
			let endTime = time.getTime() / 1000;
			let startTime = moment(this.data.startTime).unix();
			return startTime >= endTime;
		},

		//格式化请求参数
		formatRequestParams() {
			let params = { ...this.formData };
			params['id'] = this.data.id;
			return params;
		},

		handleSubmitForm() {
			this.$refs['formData'].validate((vaild) => {
				if (!vaild) return false;
				this.btnLoading = true;
				this.submitFormRequest();
			});
		},

		async submitFormRequest() {
			let parmas = this.formatRequestParams();
			let result = await piStatReality(parmas).then((res) => res.data);
			this.btnLoading = false;
			if (result.code === '0') {
				this.$showCustomMessage('success', `提交时间结果成功`);
				this.$emit('finish');
				this.visible = false;
			}
		},

		//下载模板
		handleDownloadTemp(type) {
			window.location.href = `${window.baseUrl}tbTopic/down/${type}`;
		}
	}
};
</script>

<style lang="scss" scoped>
.summary_container::v-deep {
	width: 100%;
	padding: 10px 10px 0 0;
	.el-form-item__label {
		font-size: 16px;
	}
	.el-button.temp {
		padding: 0;
		font-size: 16px;
		line-height: 1;
	}
}
</style>
